<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>重构 2 行二进制矩阵</title>
</head>
<body>
  <script>
    function fn(upper, lower, colsum) {
      var sum = 0, length = colsum.length, l2 = 0;
      var r0 = Array(length).fill(0), r1 = [...r0];
      for (var i = 0; i < length; i++) {
        sum += colsum[i];
        if (colsum[i] === 2) {
          r0[i] = r1[i] = 1;
          l2++;
        }
      }
      if (upper + lower !== sum || upper < l2 || lower < l2) return [];
      for (var i = 0; i < length; i++) {
        if (colsum[i] === 1) {
          if (upper - l2 > 0) {
            r0[i] = 1;
            upper--;
          } else {
            r1[i] = 1;
          }
        }
      }
      return [r0, r1];
    }
    console.log(fn(2, 1, [1, 1, 1]))
  </script>
</body>
</html>